package zq.cn.dao;

import android.database.Cursor;

import java.util.HashMap;
import java.util.Map;

import util.Globals;

/**
 * Created by ZQ on 2016/4/21.
 */
public class TxtDao {
    public static void insertTxtInfo(String txtPath) {
        Cursor c = getTxtInfo(txtPath);
        if (!c.moveToFirst()) {
            String sql = " insert into txt_tab(txtPath, nowPage, readFlag)" +
                    " values(?, 1, 0)";
            Globals.db.getWritableDatabase().execSQL(sql, new Object[]{txtPath});
        }
    }
    
    public static Cursor getTxtInfo(String txtPath) {
        String sql = "select * from txt_tab where txtPath=?";
        return Globals.db.getReadableDatabase().rawQuery(sql, new String[]{txtPath});
    }

    public static Map<String, Object> getByPath(String txtPath) {
        Map<String, Object> map = new HashMap<>();
        String sql = "select id, nowPage, readFlag from txt_tab where txtPath=?";
        Cursor c = Globals.db.getReadableDatabase().rawQuery(sql, new String[]{txtPath});
        c.moveToFirst();

        map.put("txtId", c.getInt(0));
        map.put("nowPage", c.getInt(1));
        map.put("readFlag", c.getInt(2));

        c.close();
        return map;
    }

    public static void updateTxtInfo(String txtPath) {
        String sql = " update txt_tab set readFlag=1 where txtPath=? ";
        Globals.db.getWritableDatabase().execSQL(sql, new Object[]{txtPath});
    }

    public static void updateNowPage(int nowPage, String txtPath) {
        String sql = " update txt_tab set nowPage=? where txtPath=? ";
        Globals.db.getWritableDatabase().execSQL(sql, new Object[] {nowPage, txtPath});
    }

}
